package com.bw.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bw.entity.Dict;
import com.bw.entity.EmployeeAdd;
import com.bw.entity.EmployeeInfo;
import com.bw.entity.EmployeeQuery;
import com.bw.mapper.EmployeeMapper;
import com.bw.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    EmployeeService employeeService;


    @RequestMapping("/list.do")
    public String list(EmployeeQuery query, Integer pageNum, Model model) {
        // 下拉框数据渲染
        selectData(model);

        //列表查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        List<EmployeeInfo> list = employeeMapper.list(query);
        PageInfo<EmployeeInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);

        // 搜索条件回显
        model.addAttribute("query", query);
        return "list";
    }

    @RequestMapping("/del.do")
    public String del(String id) {

        employeeMapper.delById(id);

        return "redirect:list.do";
    }

    /**
     * 返回添加页面
     *
     * @return
     */
    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        // 下拉框数据渲染
        selectData(model);

        return "add";
    }


    /**
     * 执行添加
     *
     * @return
     */
    @RequestMapping("/add.do")
    public String add(@Valid EmployeeAdd employeeAdd, BindingResult result, Model model) {
        // 非空验证
        if (result.hasErrors()) {
            model.addAttribute("error", result.getAllErrors().get(0).getDefaultMessage());
            //返回下拉框数据
            selectData(model);
            return "add";
        }
        //员工工号以“GF”开始，
        if (!employeeAdd.getNum().startsWith("GF")) {
            model.addAttribute("error", "员工工号以“GF”开始");
            selectData(model);
            return "add";
        }
        // 工号不能重复
        EmployeeInfo employeeInfoDB = employeeMapper.selectByNum(employeeAdd.getNum());

        if (employeeInfoDB != null) {
            model.addAttribute("error", "工号不能重复");
            selectData(model);
            return "add";
        }
        //入职时间小于当前时间
        Date currentDate = new Date();// 当前时间
        String createTimeStr = employeeAdd.getCreateTime();// 入职时间
        // 把入职时间 转为date类型
        DateTime createTime = DateUtil.parse(createTimeStr, DatePattern.NORM_DATE_PATTERN);
        // 如果入职时间 在当前时间后面
        if (createTime.isAfter(currentDate)) {
            model.addAttribute("error", "入职时间小于当前时间");
            selectData(model);
            return "add";
        }

        // 执行添加
        employeeService.add(employeeAdd);

        return "redirect:list.do";
    }

    // 给下拉框赋值
    void selectData(Model model) {
        // 下拉框数据渲染
        List<Dict> deptList = employeeMapper.listDept();
        model.addAttribute("deptList", deptList);

        List<Dict> groupList = employeeMapper.listGroup();
        model.addAttribute("groupList", groupList);
    }
}
